/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 *
 */

package com.lehe.common.utils;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 
 * @author 王丹
 */

public class EncryptUtil {

    private static final String UTF8 = "utf-8";
    //定义 加密算法,可用 DES,DESede,Blowfish  
    private static final String ALGORITHM_DESEDE = "DESede";

    /** 
     * MD5数字签名 
     *  
     * @param plainText 
     * @return 
     */
    public static String md5Digest(String plainText) {
        // 定义数字签名方法, 可用：MD5, SHA-1
        StringBuilder stringBuilder = new StringBuilder("");
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes(UTF8));
            byte b[] = md.digest();

            int i;

            for (byte element : b) {
                i = element;
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    stringBuilder.append("0");
                }
                if (i < 32) {
                    stringBuilder.append("8");
                }
                if (i < 64) {
                    stringBuilder.append("f");
                }
                stringBuilder.append(Integer.toHexString(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();
    }

    /** 
     * BASE64 加密 
     *  
     * @param src 
     * @return 
     * @throws Exception 
     */
    public static String base64Encoder(String src) throws Exception {

        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(src.getBytes(UTF8));
    }

    /** 
     * BASE64解密 
     *  
     * @param dest 
     * @return 
     * @throws Exception 
     */
    public static String base64Decoder(String dest) throws Exception {

        BASE64Decoder decoder = new BASE64Decoder();
        return new String(decoder.decodeBuffer(dest), UTF8);
    }

    /** 
     * 3DES加密 
     *  
     * @param src 
     * @param key 
     * @return 
     * @throws Exception 
     */
    public static String desedeEncoder(String src, String key) throws Exception {

        SecretKey secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESEDE);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DESEDE);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] b = cipher.doFinal(src.getBytes(UTF8));

        return byte2HexStr(b);
    }

    /** 
     * 3DES解密 
     *  
     * @param dest 
     * @param key 
     * @return 
     * @throws Exception 
     */
    public static String desedeDecoder(String dest, String key) throws Exception {

        SecretKey secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESEDE);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DESEDE);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] b = cipher.doFinal(str2ByteArray(dest));

        return new String(b, UTF8);

    }

    /** 
     * 字节数组转化为大写16进制字符串 
     *  
     * @param b 
     * @return 
     */
    private static String byte2HexStr(byte[] b) {

        StringBuilder sb = new StringBuilder();
        for (byte element : b) {
            String s = Integer.toHexString(element & 0xFF);
            if (s.length() == 1) {
                sb.append("0");
            }

            sb.append(s.toUpperCase());
        }

        return sb.toString();
    }

    /** 
     * 字符串转字节数组 
     *  
     * @param s 
     * @return 
     */
    private static byte[] str2ByteArray(String s) {

        int byteArrayLength = s.length() / 2;
        byte[] b = new byte[byteArrayLength];
        for (int i = 0; i < byteArrayLength; i++) {
            byte b0 = (byte) Integer.valueOf(s.substring(i * 2, i * 2 + 2), 16).intValue();
            b[i] = b0;
        }

        return b;
    }

    /** 
     * 构造3DES加解密方法key 
     *  
     * @param keyStr 
     * @return 
     * @throws Exception 
     */
    private static byte[] build3DesKey(String keyStr) throws Exception {

        byte[] key = new byte[24];
        byte[] temp = keyStr.getBytes(UTF8);
        if (key.length > temp.length) {
            System.arraycopy(temp, 0, key, 0, temp.length);
        } else {
            System.arraycopy(temp, 0, key, 0, key.length);
        }

        return key;
    }


    public static void main(String[] args) throws Exception {
        System.out.println(desedeEncoder("2013-01-31_00:00:000000000012true", "lcjt12#")); //加密

        //System.out.println(desedeDecoder("5D296EE63A21BC557FABBBFD5E63A1814784702E5DB85D4CA0F9509707039CBC0D465A9615A52E18", "lcjt12#"));//解密
    }
}
